package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.DateUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.zsmart.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 全程要求完成时间
 * Created by chencheng110 on 2021/02/09 12:14
 */
@Service
public class AllCompleteTimeHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        if(KiteStringUtils.isEmpty(commonAttr.getValue())||commonAttr.getValue().length()<11) {
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat(DateFormatUtils.DATE);
            String completeTime = formatter.format(date)+" 23:59:59";
//            String completeTime = formatter.format(date);
            commonAttr.setValue(completeTime);
            commonAttr.setValueDesc(completeTime);
            setCommonAttr(commonAttr);
        }
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        if (KiteStringUtils.isNotEmpty(value) && value.length() == 10){
            value += " 23:59:59";
        }

        //获取页面勾选的操作类型
        List<String> serviceOfferIdList = querySceneInstService.getExtParameterValue(sceneInstId, "WHOLE_PROCESS_TIME");
        IOperateSceneCommonService operateSceneCommonService = SpringUtil.getBean(IOperateSceneCommonService.class);
        KiteAttr kiteAttr = querySceneInstService.getCommonAttr(sceneInstId,LocalKeyConsts.BUSI_CHANGE_SCENE);
        if(Objects.nonNull(kiteAttr)){
            if(!KiteListUtils.isEmpty(serviceOfferIdList)&&serviceOfferIdList.size()==1){
                if(serviceOfferIdList.contains("1009")||serviceOfferIdList.contains("1080")){
                    kiteAttr.setIsNull("T");
                }
                else{
                    kiteAttr.setIsNull("F");
                }
            }else {
                kiteAttr.setIsNull("F");
            }
            operateSceneCommonService.setCommonAttr(sceneInstId,kiteAttr);
        }

        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        // 变更业务的场景初始化过程中不校验
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(sceneInst.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(sceneInstId, LocalKeyConsts.BUSI_CHANGE_SCENE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(sceneInstId, "initSource"))) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        //重提初始化不校验
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (Objects.nonNull(extParams) && extParams.containsKey("nodeCode")) {
            return  super.apply(sceneInstId, fieldName, value, valueDesc);
        }

        String allCompleteTimePattern = "yyyy-MM-dd HH:mm:ss";
        Date allCompleteTime = new Date();
        Date curDate = getCurrentDate();
        if (KiteStringUtils.isNotEmpty(value)){
            try{
                allCompleteTime = DateUtils.parseStrToDate(value, allCompleteTimePattern);
            }catch (Exception e){
                ErrorConsts.BUSI_COMMON_ERROR.throwOut("日期转换异常");
            }

            //全程要求完成时间不能早于当前时间
            if (DateUtil.compare(curDate, allCompleteTime) > 1){
                BssException.throwOut("", "",  "要求完成时间不能早于当前时间");
            }
        }

        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }


    public Date getCurrentDate(){
        Date toDay =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String toDayStr = sdf.format(toDay);
        Date newDate = null;
        try {
            newDate = sdf.parse(toDayStr);

        }catch (Exception e){
            return newDate;
        }
        return newDate;
    }
}
